using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using GenerateP4SpecForm.Enums;

namespace GenerateP4SpecForm
{
    public class Program
    {
        private static Mode mode;
        private static string path;

        public static void Main(string[] args)
        {
            try
            {
                SetupArguments(args);
                switch (mode)
                {
                    case Mode.Write:
                        ///create a new file from a list of options
                        string separator = args[2];
                        string formOptions = args[3];
                        SpecWriter.WriteP4SpecForm(path, formOptions.Split(separator.ToCharArray()));
                        break;

                    case Mode.Update:
                        //update an existing file via search/replace:                     
                        string searchString = args[2];
                        string replaceString = args[3];
                        SpecWriter.UpdateP4SpecForm(path, searchString, replaceString);
                        break;

                    case Mode.Append:
                        //update an existing file by adding the given string, if it doesn't exist:
                        string additionalString = args[2];
                        SpecWriter.AppendToP4SpecForm(path, additionalString);
                        break;

                    default:
                        throw new ApplicationException("Unknown mode");
                }
                Console.WriteLine(string.Format("GenerateP4SpecForm: SUCCESS. {0} completed: {1}", EnumHelper.GetName(mode), path));
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("GenerateP4SpecForm: FAILURE. Message: {0}", ex.Message));
            }
        }

        private static void SetupArguments(string[] args)
        {
            //determine the mode:
            mode = Mode.Null;
            if (args.Length > 0)
            {
                mode = EnumHelper.GetEnumFromName<Mode>(args[0]);
            }

            //validate remaining arguments:
            switch (mode)
            {
                case Mode.Null:
                    throw new ApplicationException("Unknown mode");
                    break;

                case Mode.Append:
                    if (args.Length != 3)
                    {
                        throw new ApplicationException("Expected arguments: mode, path, additionalString");
                    }
                    break;

                case Mode.Update:
                    if (args.Length != 4)
                    {
                        throw new ApplicationException("Expected arguments: mode, path, searchString, replaceString");
                    }
                    break;

                case Mode.Write:
                    if (args.Length != 4)
                    {
                        throw new ApplicationException("Expected arguments: mode, path, separator, formOptions");
                    }
                    break;
            }

            path = args[1];
        }
    }
}
